import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import { Exclude, Expose, Transform } from 'class-transformer';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  @ApiProperty({ description: '用户id', example: 1 })
  id: number;

  @Column({ unique: true, length: 50 })
  @ApiProperty({ description: '用户名', example: 'admin' })
  username: string;
  @ApiProperty({ description: '手机号', example: '16655255546' })
  @Column({ length: 11, nullable: true })
  @Transform(({ value }: any) =>
    value ? value.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3') : value,
  )
  mobile: string;

  @Expose()
  @ApiProperty({ description: '联系方式', example: '邮箱：admin@163.com' })
  get contact(): string {
    return `邮箱：${this.email}`;
  }

  @Column({ length: 100, nullable: true })
  @ApiProperty({ description: '邮箱', example: 'admin@163.com' })
  email: string;

  @Column()
  @ApiHideProperty()
  @Exclude()
  password: string;

  @Column({ default: 1 })
  @ApiProperty({ description: '状态', example: 1 })
  status: number;
  @Column({ default: false })
  @ApiProperty({ description: '是否是超级管理员', example: false })
  isSuper: boolean;
  @Column({ default: 100 })
  @ApiProperty({ description: '排序', example: 100 })
  sort: number;
  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  @ApiProperty({ description: '创建时间', example: '2023-07-01 00:00:00' })
  createdAt: Date;
  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  @ApiProperty({ description: '更新时间', example: '2023-07-01 00:00:00' })
  updatedAt: Date;
  @Column({ default: false })
  @ApiProperty({ description: '是否删除', example: false })
  isDeleted: boolean;
}
